<template>
	<view class="page">
		<template v-if="isLogin">
			<view class="category-list row-2">
				<navigator url="/package_zhinengcipan/list/list" hover-class="none" class="item">
					<view class="icon">
						<image src="@/static/images/ysb_datas.png" mode="widthFix"></image>
					</view>
					<view class="text">设备列表</view>
				</navigator>
				<navigator url="/package_zhinengcipan/search-data/search-data" hover-class="none" class="item">
					<view class="icon">
						<image src="@/static/images/data_search.png" mode="widthFix"></image>
					</view>
					<view class="text">数据查看</view>
				</navigator>
			</view>
			<view class="start-btn-area">
				<!-- <navigator url="/package_zhinengcipan/form/form" hover-class="none" class="btn"> -->
				<view class="btn" @click="scan">
					<image src="@/static/images/fk_scan_start.png" mode="widthFix"></image>
				</view>
				<!-- </navigator> -->
			</view>
			<!-- <navigator url="/package_zhinengcipan/form/form?open_type=2">
				<button style="width: 100px;height: 40px;border: 1px solid black;">1</button>
			</navigator>
			<navigator url="/package_zhinengcipan/form/form?open_type=1">
				<button style="width: 100px;height: 40px;border: 1px solid black;">0</button>
			</navigator>
			<navigator :url="`/package_zhinengcipan/detail/detail?number=${1}&dev_id=${1}`">
				<button style="width: 100px;height: 40px;border: 1px solid black;">111</button>
			</navigator> -->


		</template>

		<view v-if="flag.isFullPage" class="full-fix-page">
			<!-- 修改名称 -->
			<view v-if="flag.isLoginAdmin" class="popup-wrap a-fadein">
				<view class="popup-type-model ">
					<view class="close-btn iconfont f-s34" @click="showPopup('hide')">&#xe624;</view>
					<view class="popup-body">
						<view class="title">密码验证</view>
						<view class="content get-code-popup">
							<view class="code-bar">
								<input type="text" v-model="dev_password" placeholder="请输入密码">
							</view>
							<view class="code-comfirm-btn" @click="login">确定</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		HTTP_OLD_HOST,
		WEB_HOST
	} from '@/config.js'
	import {
		dateFormat,
		uploadImage,
		uploadToServe,
	} from '@/common/util.js'
	export default {
		name: 'name',
		data() {
			return {
				isLogin: false,
				token: uni.getStorageSync('token'),
				appid: getApp().globalData.appid,
				userInfo: uni.getStorageSync('userInfo'),
				loginStateUpdate: false,
				app_name: getApp().globalData.app_name,
				files: [],
				flag: { // 弹窗相关
					isFullPage: false,
					isLoginAdmin: false
				},
				dev_password: '',
				number: '',
				send_data: null
			}


		},
		async onLoad() {
			this.isLogin = await this.$loginState('check');
		},
		async onShow() {
			// let str ='http://new.agri120.org/report/report.html?devnumber=d4ff97c6b6ec11ebac1acf793133beb8'
			// str.split('?')[1].split('&').reduce((pre,cur)=>{
			// 	pre
			// },{})
		},
		onUnload() {
			// this.flag.isFullPage = false;
			// this.flag.isChangeType = false
		},
		onHide() {
			// this.flag.isFullPage = false;
			// this.flag.isChangeType = false
		},
		methods: {

			scan() {
				let _this = this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: async (res) => {
						console.log(res);
						let number = res.result.split('=')[1].replace(/[\n]/g, '');
						let scan_res = await _this.$api.zncp_api.scan({ number: number, token: this.token, appid: this.appid }).catch(err => console.log('scan-err', err));
						console.log('scan_res', scan_res);
						if (scan_res.data.ret == 1) {
							let n = scan_res.data?.data ?? '';
							uni.navigateTo({
								url: `/package_zhinengcipan/form/form?open_type=1&number=${number}&device_number=${n}`
							})
						} else if (scan_res.data.ret == 0) {
							let data = encodeURIComponent(JSON.stringify(scan_res.data.data));
							if (scan_res.data.data.is_jh == 0) {
								this.number = number;
								this.showPopup('show', 'isLoginAdmin');
								this.send_data = data;
								// _this.login(number, data);
								// uni.navigateTo({
								// 	url: '/package_zhinengcipan/form/form?open_type=2&data=' + data
								// })
							} else {
								uni.showActionSheet({
									itemList: ['查看数据', '继续投放'],
									success(sheet_res) {
										console.log(sheet_res);
										if (sheet_res.tapIndex == 0) {
											let { number: n, id: dev_id } = scan_res.data.data;
											uni.navigateTo({
												url: `/package_zhinengcipan/detail/detail?number=${n}&dev_id=${dev_id}`
											})
										}
										if (sheet_res.tapIndex == 1) {
											console.log('number', number);
											// let { number: n} = scan_res.data.data;
											uni.navigateTo({
												url: `/package_zhinengcipan/form/form?open_type=2&data=${data}`
											})
										}
									}
								})
							}
						} else {
							uni.showToast({
								icon: 'error',
								image: '../../static/images/error.png',
								title: scan_res.data.message
							})
						}
					},
					fail: err => {
						console.log('scan', err);
					}
				})
			},

			async login() {
				if (!this.dev_password) {
					return uni.showToast({
						icon: 'error',
						image: '../../static/images/error.png',
						title: '请输入密码'
					})
				}
				let scan_res = await this.$api.zncp_api.scan({
					number: this.number,
					password: this.dev_password,
					type: 1
				})
				console.log(scan_res);
				if (scan_res.data.ret == 0) {
					this.showPopup('hide');
					uni.navigateTo({
						url: `/package_zhinengcipan/form/form?open_type=2&data=${this.send_data}`
					})
				} else {
					setTimeout(() => {
						uni.showToast({
							icon: 'error',
							image: '../../static/images/error.png',
							title: scan_res.data.message,
							mask: true,
							duration: 1000,
						})
					}, 300)
				}
				return
				uni.showModal({
					title: '密码验证',
					editable: true,
					placeholderText: '请输入设备密码',
					showCancel: false,
					success: async (res) => {
						console.log('login', res);
						if (res.confirm) {
							let scan_res = await this.$api.zncp_api.scan({
								number: n,
								password: res.content,
								type: 1
							})
							console.log(scan_res);
							if (scan_res.data.ret == 0) {
								uni.navigateTo({
									url: `/package_zhinengcipan/form/form?open_type=2&data=${data}`
								})
							} else {
								this.login(n, data);
								setTimeout(() => {
									uni.showToast({
										icon: 'error',
										image: '../../static/images/error.png',
										title: scan_res.data.message,
										mask: true,
										duration: 1000,
									})
								}, 300)
							}
						}
					},
					fail: err => {

					}
				})
			},

			addDev() {
				uni.navigateTo({
					url: '/package_shibie/dataList/dataList'
				})
			},

			installDev() {

			},

			checkPhoto: async function() {
				// 上传现场照片
				let _this = this;
				uploadImage({
					length: _this.files.length,
					sourceType: ['album'],
					maxSize: 1,
					async success(res) {
						let params_data = {};
						params_data.status = 0;
						params_data.url = res.tempFilePaths[0];
						_this.files.push(params_data);
						let token = await _this.$api.user_api.getToken({
							appid: '100002',
							appsecret: '24dcb652ce0043c9a53d90de2468b5b8'
						})
						uni.showLoading();
						uploadToServe({
							type: 1,
							url: 'https://shibie.honghuoyi.com/user/sb-insert.do',
							filePath: res.tempFilePaths[0],
							name: 'myfile',
							formData: {
								appid: 100002,
								token: token.data.data.token,
								uid: _this.userInfo.id
							}
						}).then((up_res) => {
							uni.hideLoading();
							if (up_res.ret == 1000) {
								uni.navigateTo({
									url: '/package_shibie/dataList/dataList'
								})
							}
						})

						// uploadToServe({
						// 	url: '/yfk/upload-file',
						// 	filePath: res.tempFilePaths[0],
						// 	name: 'photo',
						// 	formData: {
						// 		'p_id': 0,
						// 		'uid': _this.userInfo.id
						// 	}
						// }).then((up_res) => { 
						// 	if (up_res.ret == 0) {								
						// 		params_data.file_name = up_res.data.file_name;
						// 		params_data.file_path = up_res.data.file_path;
						// 		params_data.file_type = up_res.data.file_type;
						// 		params_data.status = up_res.data.status;
						// 		params_data.type = up_res.data.type; 								
						// 	}
						// }).catch((err)=>{
						// 	console.log(err)
						// })
					}
				})

			},

			// 打开弹窗
			showPopup: function(t, n) {
				if (t == 'show') {
					this.flag.isFullPage = true;
					this.flag[n] = true;
				} else if (t == 'hide') {
					for (let i in this.flag) {
						this.flag[i] = false;
					}
				}
			},

		},
		components: {

		},

		computed: {

		},
		watch: {

		}

	}
</script>

<style lang="less" scoped>
	.category-list {
		background: #fff;
		margin-bottom: 20rpx;
		padding: 20rpx 0 0;

		.item {
			.icon {
				width: 30% !important;
			}
		}
	}

	.name {
		text-align: center;

		text {
			padding-top: 10rpx;
			border-top: 2rpx dashed #999;
			font-size: 28rpx;
			color: #999;
		}
	}

	.start-btn-area {
		position: absolute;
		left: 0;
		bottom: 150rpx;
		width: 100%;
	}

	.btn {
		width: 60%;
		margin: 0 auto;

		image {
			width: 100%;
		}
	}

	.check-photo {
		text-align: center;
		font-size: 28rpx;
	}

	.code-bar {
		display: flex;
		height: 80rpx;
		border: 1rpx solid #eee;
		border-radius: 15rpx;
		margin-bottom: 40rpx;

		input {
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
			padding: 0 10rpx;
			font-size: 30rpx;
		}

		.get-code-btn {
			flex-shrink: 0;
			width: 35%;
			line-height: 80rpx;
			text-align: center;
			font-size: 30rpx;
			color: #09f;
		}
	}

	.get-code-popup {
		.phone-text {
			margin-bottom: 30rpx;
			color: #666;
		}

		.code-comfirm-btn {
			height: 70rpx;
			line-height: 70rpx;
			margin: 0 auto;
			background: linear-gradient(#e96b5c, #ce3c2f);
			border-radius: 8rpx;
			color: #fff;
			text-align: center;
			font-size: 30rpx;
		}
	}
</style>